package com.sxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxy.controller.common.CustomException;
import com.sxy.dao.SetmealDao;
import com.sxy.dto.SetmealDto;
import com.sxy.pojo.Setmeal;
import com.sxy.pojo.SetmealDish;
import com.sxy.service.SetmealDishService;
import com.sxy.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 宋鑫宇
 * @version 1.0
 */
@Slf4j
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealDao, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Value("${syn.path}")
    private String basePath;

    /**
     * 添加套餐,并保存套餐和菜品的关联关系
     *
     * @param setmealDto
     */
    @Transactional
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //保存基本信息
        this.save(setmealDto);
        //取出SetmealDish对象集合
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        //遍历集合取出每个SetmealDish对象设置当前的setmealId值，执行查询
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        setmealDishService.saveBatch(setmealDishes);
    }

    @Transactional
    @Override
    public void removeWithDish(List<Long> ids) {
        Setmeal setmeal;
        //查询条件套餐状态是否可用
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.in(Setmeal::getId, ids);
        lqw.eq(Setmeal::getStatus, 1);
        //执行查询，返回int值
        int count = this.count(lqw);
        if (count > 0) {
            throw new CustomException("套餐还没有停售，不能删除");
        }
        for (Long id : ids) {
            setmeal = this.getById(id);
            //删除保存的图片数据
            String image = setmeal.getImage();
            new File(basePath + image).delete();
        }
        this.removeByIds(ids);

        //删除对应关系表中的数据
        LambdaQueryWrapper<SetmealDish> lqw1 = new LambdaQueryWrapper<>();
        lqw1.in(SetmealDish::getSetmealId, ids);

        //执行业务层方法进行删除
        setmealDishService.remove(lqw1);

    }

    /**
     * 通过id查询对应的套餐信息以及关联菜品的信息
     *
     * @param id
     * @return
     */
    @Override
    public SetmealDto getByIdWithDish(Long id) {
        //通过id查询基本数据
        Setmeal setmeal = this.getById(id);
        //创建SetmealDto对象
        SetmealDto setmealDto = new SetmealDto();
        //拷贝对象
        BeanUtils.copyProperties(setmeal, setmealDto);

        //创建条件查询器
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        //添加条件
        lqw.eq(SetmealDish::getSetmealId, id);
        //执行业务层方法
        List<SetmealDish> setmealDishes = setmealDishService.list(lqw);

        //将查询到的关联数据封装到setmealDto对象中
        setmealDto.setSetmealDishes(setmealDishes);

        return setmealDto;
    }

    /**
     * 修改对应id的套餐属性和关联的菜品信息
     * @param setmealDto
     */
    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        //更新基本数据
        this.updateById(setmealDto);

        //先删除对应关联的菜品信息，根据id删除
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(lqw);

        //从setmealDto对象中取出关联菜品数据
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        //遍历setmealDishes集合，将setmealId存入到集合中
        setmealDishes = setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        //执行业务层方法存储关联菜品数据
        setmealDishService.saveBatch(setmealDishes);
    }
}

